package com.gz.university.crm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gz.university.crm.dao.ISysRoleDao;
import com.gz.university.crm.entity.SysRole;
import com.gz.university.crm.entity.SysUserGroup;
import com.gz.university.crm.form.SysRoleForm;
import com.gz.university.crm.service.ISysRoleService;

@Transactional(readOnly = true)
@Service(value = ISysRoleService.SERVICE_NAME)
public class SysRoleServiceImpl implements ISysRoleService {
	private ISysRoleDao sysRoleDao;

	public ISysRoleDao getSysRoleDao() {
		return sysRoleDao;
	}

	@Resource(name = ISysRoleDao.DAO_NAME)
	public void setSysRoleDao(ISysRoleDao sysRoleDao) {
		this.sysRoleDao = sysRoleDao;
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
	@Override
	public void saveSysrole(SysRole sysRole) {
		// TODO Auto-generated method stub
		try {
			this.sysRoleDao.save(sysRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}

	@Override
	public List<SysRole> findSysRoles(SysRoleForm sysRoleForm) {
		// TODO Auto-generated method stub
		String hql = "from SysRole t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(sysRoleForm, hql, params);
		List<SysRole> list = this.sysRoleDao.find(hql, params);
		return list;
	}

	private String addWhere(SysRoleForm sysRoleForm, String hql,
			Map<String, Object> params) {
		// TODO Auto-generated method stub
		if (StringUtils.isNotBlank(sysRoleForm.getName())) {
			hql += " and t.name like :name";
			params.put("name", "%%" + sysRoleForm.getName() + "%%");
		}
		return hql;
	}

	@Override
	public SysRole findSysRoleById(Integer sysRoleId) {
		// TODO Auto-generated method stub
		String hql = "from SysRole r where r.sysRoleId is :sysRoleId";
		Map<String, Object> params = new HashMap<>();
		params.put("sysRoleId", sysRoleId);
		List<SysRole> list = this.sysRoleDao.find(hql, params);
		SysRole result = null;
		if (list != null && list.size() > 0) {
			result = list.get(0);
		}
		return result;
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
	@Override
	public void updateSysRole(SysRole sysRole) {
		// TODO Auto-generated method stub
		this.sysRoleDao.update(sysRole);
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
	@Override
	public void deleteSysRolesByIds(Integer[] deleteIds) {
		// TODO Auto-generated method stub
        this.sysRoleDao.deleteSysRoles(deleteIds);
	}

	@Override
	public List<SysRole> findAllSysRoles() {
		// TODO Auto-generated method stub
		String hql="from SysRole s order by s.sysRoleId";
		return this.sysRoleDao.find(hql);
	}

}
